package com.braveman.im.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import com.alibaba.fastjson.JSON;
import com.ht.baselibs.application.HTApplication;
import com.ht.baselibs.bean.BaseGetBean;
import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import com.braveman.im.R;
import com.braveman.im.constant.ConstantPool;
import com.braveman.im.dao.AccountDao;
import com.braveman.im.ui.activity.ResultActivity;
import com.braveman.im.ui.dialog.TipsDialog;
import com.braveman.im.utils.NotificationUtils;
import com.braveman.im.utils.VoiceUtils;

import de.greenrobot.event.EventBus;
import woyou.aidlservice.jiuiv5.ICallback;
import woyou.aidlservice.jiuiv5.IWoyouService;

public class GetuiService extends GTIntentService {

    public static final String GETUI_CLIEND_ID = "getui_cliend_id";

    public static final int AUTHOR_SUCCESS = 100;
    public static final int AUTHOR_FAIL = 101;

    public static final int GET_MONEY = 200;
    public static final int CHANGE_PWD = 300;
    private IWoyouService woyouService;
    private TipsDialog mTipsDialog;

    @Override
    public void onReceiveServicePid(Context context, int i) {

    }


    @Override
    public void onCreate() {
        super.onCreate();

        Intent intent = new Intent();
        intent.setPackage("woyou.aidlservice.jiuiv5");
        intent.setAction("woyou.aidlservice.jiuiv5.IWoyouService");
        startService(intent);//启动打印服务
        try {
//            bindService(intent, connService, Context.BIND_AUTO_CREATE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onReceiveClientId(final Context context, String clientid) {
        SharedPreferencesUtils.put(GETUI_CLIEND_ID, clientid);
        Intent downloadIntent = new Intent(context, PushService.class);
        context.startService(downloadIntent);
    }

    @Override
    public void onReceiveMessageData(final Context context, GTTransmitMessage msg) {
//        String currentDate = AbDateUtil.getCurrentDate(AbDateUtil.dateFormatYMDHMS);
//        Log.e("currentDate: ",currentDate);
        byte[] payload = msg.getPayload();
        if (payload != null) {
            String data = new String(payload);
            BaseGetBean baseGetBean = JSON.parseObject(data, BaseGetBean.class);
            Intent intent;
            switch (baseGetBean.code) {
                case AUTHOR_SUCCESS:
                    AccountDao.getInstance().authorStatus(1);
                    EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_AUTHOR_SUCCESS));
                    intent = new Intent(context, ResultActivity.class);
                    intent.putExtra(ResultActivity.RESULT_TITLE, "恭喜您通过审核");
                    intent.putExtra(ResultActivity.RESULT_MSG, baseGetBean.message);
                    NotificationUtils.show(context, R.mipmap.ic_launcher, HTApplication.getContext().getString(R.string.app_name), baseGetBean.message, intent, true);
                    break;
                case AUTHOR_FAIL:
                    AccountDao.getInstance().authorStatus(3);
                    EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_AUTHOR_SUCCESS));
                    break;
                case CHANGE_PWD:
                    EventBus.getDefault().post(new EventCenter(EventCode.HSY_REQUEST_CODE_UNLOGIN,baseGetBean.message));
                    break;
                case GET_MONEY:
                    int notificationType = SharedPreferencesUtils.getInt(ConstantPool.NOTIFICATION_TYPE, 0);
                    if (notificationType == 0) {
//                        try {
//                            if (woyouService != null) {
//
//                                woyouService.lineWrap(2, callback);
//                                woyouService.setAlignment(0, callback);
//                                woyouService.printTextWithFont("支付成功", "gh", 54,
//                                        callback);
//                                woyouService.lineWrap(1, callback);
//                                woyouService.printText("--------------------------------", callback);
//                                woyouService.lineWrap(1, callback);
//                                woyouService.printText("" + baseGetBean.resultMessage.split(", ")[0], callback);
//                                woyouService.printText("--------------------------------", callback);
//                                woyouService.lineWrap(3, callback);
//                            }
//                        } catch (RemoteException e) {
//                            e.printStackTrace();
//                        }

                        VoiceUtils.playDetails(baseGetBean.message.split("，")[0]);
                    } else if (notificationType == 1) {
                        VoiceUtils.play();
                    }
                    EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_BALANCE_CHANGE));
                    EventBus.getDefault().post(new EventCenter(EventCode.EVENT_REFRESH_SELFFRAGMENT_BALANCE));
                    try {
                        intent = new Intent();
                        NotificationUtils.show(context, R.mipmap.ic_launcher, HTApplication.getContext().getString(R.string.app_name), baseGetBean.message, intent, false);
//                        NotificationUtils.send(context,intent,R.mipmap.ic_launcher,);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    break;
            }
        }
//        String taskid = msg.getTaskId();
//        String messageid = msg.getMessageId();
//        PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001);
    }

    @Override
    public void onReceiveOnlineState(Context context, boolean b) {
        if (!b) {
            EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_GETUI_LINE));
        } else {
            EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_GETUI_ONLINE));
        }
    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage gtCmdMessage) {

    }


    private ServiceConnection connService = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            woyouService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            woyouService = IWoyouService.Stub.asInterface(service);
        }
    };

    ICallback callback = new ICallback.Stub() {
        @Override
        public void onRunResult(boolean success) throws RemoteException {
        }

        @Override
        public void onReturnString(final String value) throws RemoteException {
        }

        @Override
        public void onRaiseException(int code, final String msg)
                throws RemoteException {
        }
    };
}
